home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Mac / Lib / test / ctbtest.py < prev    next >
Encoding:
Text File  |  2000-06-23  |  1.1 KB  |  51 lines

  1. #
  2. # Simple test program for ctb module: emulate a terminal.
  3. # To simplify matters use the python console window for output.
  4. #
  5. import ctb
  6. import Evt
  7. import Events
  8. import MacOS
  9. import sys
  10.  
  11. def cb(err):
  12.     print 'Done, err=', err
  13.  
  14. def main():
  15.     if not ctb.available():
  16.         print 'Communications Toolbox not available'
  17.         sys.exit(1)
  18.     # Disable Python's event processing (we do that)
  19.     MacOS.SchedParams(1, 0)
  20.     print 'Minimal terminal emulator V1.0'
  21.     print '(type command-Q to exit)'
  22.     print
  23.     
  24.     l = ctb.CMNew('Serial Tool', None)
  25.     l.Open(10)
  26.     l.SetConfig(l.GetConfig() + ' baud 4800')
  27.     
  28.     while 1:
  29.         l.Idle()    # Give time to ctb
  30.         
  31.         ok, evt = Evt.WaitNextEvent(0xffff, 0)
  32.         if ok:
  33.             what, message, when, where, modifiers = evt
  34.             
  35.             if what == Events.keyDown:
  36.                 # It is ours. Check for command-. to terminate
  37.                 ch = chr(message & Events.charCodeMask)
  38.                 if ch == 'q' and (modifiers & Events.cmdKey):
  39.                     break
  40.                 l.Write(ch, ctb.cmData, -1, 0)
  41.         d, dummy = l.Read(1000, ctb.cmData, 1)
  42.         if d:
  43.             for ch in d:
  44.                 if ch != '\r':
  45.                     sys.stdout.write(ch)
  46.             sys.stdout.flush()
  47.     l.Close(-1, 1)
  48.     del l
  49.             
  50. main()
  51.